package com.hnzq.cloud.sendassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.IO.AddCarIO;
import com.hnzq.cloud.IO.entity.FreCar;
import com.hnzq.cloud.sendassembly.mapper.FreCarMapper;
import com.hnzq.cloud.sendassembly.service.FreCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnzq.cloud.util.ComUtil;
import com.hnzq.cloud.util.FreException;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2022-02-07
 */
@Service
public class FreCarServiceImpl extends ServiceImpl<FreCarMapper, FreCar> implements FreCarService {

    @Override
    public boolean addFreCar(AddCarIO addCarIO) {
        FreCar freCar = ComUtil.copyProperties(addCarIO, FreCar.class);
        if (save(freCar)) {
            return true;
        }
        return false;
    }

    @Override
    public FreCar getByUserId(Integer id) throws FreException {
        QueryWrapper<FreCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        FreCar one = getOne(queryWrapper);
        if (one==null)
            throw new FreException("该司机没有车");
        return one;
    }

    @Override
    public boolean editFreCar(AddCarIO addCarIO) {
        FreCar byId = getById(addCarIO.getCarId());
        if (byId==null)
            return false;
        FreCar freCar = ComUtil.copyProperties(addCarIO, FreCar.class);
        QueryWrapper<FreCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id",freCar.getCarId());
        if (update(freCar,queryWrapper)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteFreCar(Integer id) {
        //判断是否闲置状态后再删除
        QueryWrapper<FreCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id",id);
        return remove(queryWrapper);
    }
}
